﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileManagerBase;
using System.Windows.Forms;
using System.IO;
using FileManagerLibrary.Base;
using FileManagerLibrary.Base.FileBase;
using FileManagerLibrary.Base.UIData;
using FileManagerLibrary;
using System.Collections.Specialized;
using Logging;

namespace FileManagerTool
{
    public abstract class BackupJobManagerControllerBase
    {
        internal BackupJob CurrentJob { get; set; }
        public BackupJobManagerView View { get; set; }
        public bool IsEditMode { get; set; }

        public BackupJobManagerControllerBase(BackupJobManagerView view, BackupJob currentJob,bool isEditMode)
        {
            View = view;
            CurrentJob = currentJob;
            IsEditMode = isEditMode;
        }

        protected internal string GetBackupLocation(string txt)
        {
            string x = FileOperations.Getfolder(txt);
            if (x.Length > 0)
                txt = x;
            else
                txt = "";
            return txt;
        }

        protected internal bool DeleteSelctedItem(ListBox lst)
        {
            if (lst.SelectedItems.Count == 1)
            {
                lst.Items.Remove(lst.SelectedItems[0]);
                return true;
            }
            else
            {
                return false;
            }
        }

        protected internal string GetDirectoryFromClipBoard()
        {
            string path ="";
            if (Clipboard.ContainsFileDropList() == true)
            {
                System.Collections.Specialized.StringCollection files = Clipboard.GetFileDropList();
                if (files.Count != 1)
                {
                    Global.MessageBoxShow("Invalid Path in ClipBoard");
                    return "";
                }
                else
                    path = files[0];
            }
            else if(Clipboard.ContainsText() == true)
                path = Clipboard.GetText();
            if (Directory.Exists(path) == true)
                return path;
            else
            {
                Global.MessageBoxShow("Invalid Path in ClipBoard");
                return "";
            }
        }

        protected internal FileSystemCollection GetFileSystemListFromClipBoard()
        {
            string path;
            if (Clipboard.ContainsFileDropList() == true)
            {
                System.Collections.Specialized.StringCollection files = Clipboard.GetFileDropList();
                return new FileSystemCollection(files);
            }
            else if (Clipboard.ContainsText() == true)
            {
                path = Clipboard.GetText();
                if (path == null)
                    return new FileSystemCollection();
                else
                {
                    StringCollection strarr = new StringCollection();
                    strarr.AddRange(path.Split(new string[] { Environment.NewLine.ToString() }, StringSplitOptions.RemoveEmptyEntries));
                    for (int i = strarr.Count ; i > 0 ; i--)
			        {
                        string item = strarr[i-1];
                        if (File.Exists(item) == true || Directory.Exists(item) == true)
                            continue;
                        else
                            strarr.Remove(item);
                    }
                    return new FileSystemCollection(strarr);
                }
            }
            else
            {
                Global.MessageBoxShow("Invalid Path in ClipBoard");
                return new FileSystemCollection();
            }
        }

        public abstract bool SaveData();
        public abstract void ReloadData();

        /// <summary>
        /// Abstract method to be overwirtten with logic for validating a section of page/form/tab/expander before user can accepts value to 
        /// next block of the same page/form/tab/expander.
        /// </summary>
        /// <param name="level">passes the current level upto which the Validate operation must be performed. 
        /// value = 0 means all levels need to be checked and higher values means less check</param>
        /// <returns></returns>
        public abstract bool ValidateContents(LevelsEnum level);
    }

    public enum LevelsEnum
    {
        Final = 0,
        Level1 =1,
        Level2 =2,
        Level3 =3,
        Level4 = 4
    };

    public class FileSystemCollection
    {
        public List<String> Files { get; set; }
        public List<String> Folders { get; set; }

        public FileSystemCollection()
        {
            Files = new List<string>();
            Folders = new List<string>();
        }

        public FileSystemCollection(System.Collections.Specialized.StringCollection fileobjects)
        {
            Files = new List<string>();
            Folders = new List<string>();
            foreach (var item in fileobjects)
            {
                try
                {
                    FileAttributes flatr = File.GetAttributes(item);
                    if (flatr == FileAttributes.Directory)
                    {
                        Folders.Add(item);
                    }
                    else if (File.Exists(item))
                    {
                        Files.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    Logger.StaticLogger.AddErrorLogEntry(ex, "Error in adding files or filders to list. Name: " + item + ".");
                }
            }    
        }

    }
}
